<template>
	<view class="container">
		<view class="form-box">
			<view class="form">
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">收货人</view>
						 <input class="input" placeholder="请输入收货人" v-model="form.acceptName" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">性别</view>
						<view class="radio-group">
							<view class="radio" :class="{'checked': !form.sex}" style="margin-right: 10rpx;" @tap="form.sex=0">先生</view>
							<view class="radio" :class="{'checked': form.sex}" @tap="form.sex=1">女士</view>
						</view>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">联系方式</view>
						 <input class="input" placeholder="请输入收货人联系方式" v-model="form.mobile" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">收货地址</view>
						 <input class="input" placeholder="请选择收货地址" v-model="form.street" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">门牌号</view>
						 <input class="input" placeholder="请输入收货人详细地址" v-model="form.doorNumber" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
			</view>
			<view class="btn-section">
				<button type="primary" size="default" @tap="save">保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	import listCell from '@/components/list-cell/list-cell'
	import $http from '@/common/api/request.js'
	
	export default {
		components: {
			listCell
		},
		data() {
			return {
				current_id:'',
				// {
				// 	"deleted": 0,
				// 	"id": 1,
				// 	"isDefault": 1,
					
				// }
				form: {
					id:this.current_id,
					acceptName: '',
					sex: 0,
					mobile: '',
					street: '',
					doorNumber: ''
				}
			}
		},
		async onLoad({id}) {
			//为了方便演示，这里用本地缓存
			if(id) {
				this.current_id=id
				console.log('地址从前一个页面传过来的，等待修改对象的id：',this.current_id)
				console.log('id的类型：',typeof(this.current_id))
				//显示需要被修改的表单信息，从上一个界面拿到的
				$http.request({
							url:"/addresses/"+this.current_id,
							method:"GET"
						}).then((res)=>{
							if(res.code == 200){
								console.log("等待修改的addresses:");
								console.log(res.data);
								this.form = res.data;
							}else{
								uni.showToast({
									title:'地址查询失败，请重试',
									icon:'none'
								})
								}
						}).catch(()=>{
							uni.showToast({
								title:'"/addresses/"+this.current_id接口获取失败，请重试',
								icon:'none'
							})	
				})
				
				// this.form = this.$store.state.addresses.find(item => item.id == id)
			}
		},
		methods: {
			save() {	
				console.log("即将被插入的数据如下：")
				console.log(this.form)
				if(this.current_id){
					//修改
					$http.request({
								url:"/addresses/update",
								method:"PUT",
								data: this.form
							}).then((res)=>{
								if(res.code == 200){
									console.log("被修改的数据如下:addresses");
									console.log(res.data);
									uni.showToast({
										title:'地址修改成功',
										icon:'none'
									})
									
								}else{
									uni.showToast({
										title:'地址修改失败，请重试',
										icon:'none'
									})
									}
							}).catch(()=>{
								uni.showToast({
									title:'/addresses/update接口获取失败，请重试',
									icon:'none'
								})	
					})
					
					
				}else{
					//增加
					delete this.form.id
					console.log("去除id后新的被传对象：",this.form)
					$http.request({
								url:"/addresses/add",
								method:"POST",
								data: this.form
							}).then((res)=>{
								if(res.code == 200){
									console.log("被插入的数据如下:addresses");
									console.log(res.data);
									uni.showToast({
										title:'地址添加成功',
										icon:'none'
									})
									
								}else{
									uni.showToast({
										title:'地址添加失败，请重试',
										icon:'none'
									})
									}
							}).catch(()=>{
								uni.showToast({
									title:'/addresses/add接口获取失败，请重试',
									icon:'none'
								})	
					})
					
				}
					
			uni.$emit('up2dateAddress',null)
			// uni.navigateBack()	
			uni.navigateTo({
				url: '/pages/address/address'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
	.form-box {
		width: 100%;
		height: 100%;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		color: $text-color-base;
		
		.form-input {
			display: flex;
			align-items: center;
			width: 100%;
		}
		
		.label {
			width: 200rpx;
			font-size: $font-size-lg;
			color: $text-color-base;
			font-weight: 500;
		}
		
		.input {
			flex: 1;
			display: flex;
			align-items: center;
		}
		
		.radio-group {
			display: flex;
			justify-content: flex-start;
			
			.radio {
				padding: 10rpx 30rpx;
				border-radius: 6rpx;
				border: 2rpx solid $text-color-assist;
				color: $text-color-assist;
				font-size: $font-size-base;
				
				&.checked {
					background-color: $color-primary;
					color: $text-color-white;
					border: 2rpx solid $color-primary;
				}
			}
		}
		
		.btn-section {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			
			button {
				font-size: $font-size-base;
				height: 90rpx;
				border-radius: 50rem !important;
				width: 85%;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
